package com.lujianfei.androidthread.threadsActivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.lujianfei.androidthread.R;

import java.lang.ref.WeakReference;

public class HandlerThreadActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "HandlerThreadActivity";
    private static final int TYPE_BACKGROUND_UPDATE = 1;
    private static final int TYPE_UI_UPDATE = 2;
    private HandlerThread handlerThread;
    private MyHandler workHandler;
    private MyHandler uiHandler;
    private TextView txt_info;
    private int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_thread);
        txt_info = findViewById(R.id.txt_info);
        findViewById(R.id.bt_clickme).setOnClickListener(this);
        // 声明并启动 HandlerThread
        handlerThread = new HandlerThread("MyHandlerThread");
        handlerThread.start();
        // 把 handlerThread 的 Looper 给到 Handler
        workHandler = new MyHandler(handlerThread.getLooper(),this);
        // 另外创建一个更新 UI 专用的 Handler
        uiHandler = new MyHandler(this);
    }

    @Override
    protected void onDestroy() {
        handlerThread.quit();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        // 子线程 （单线程）
        Message message = workHandler.obtainMessage();
        message.arg1 = TYPE_BACKGROUND_UPDATE;
        message.arg2 = count;
        message.sendToTarget();

        // UI线程
        message = uiHandler.obtainMessage();
        message.arg1 = TYPE_UI_UPDATE;
        message.arg2 = count;
        message.sendToTarget();

        ++ count;
    }

    static class MyHandler extends Handler {
        // 持有弱引用HandlerThreadActivity,在合适的时机，GC回收时会被回收掉，避免内存泄漏
        private final WeakReference<HandlerThreadActivity> mActivity;

        public MyHandler(HandlerThreadActivity mainActivity){
            super();
            mActivity =new WeakReference<HandlerThreadActivity>(mainActivity);
        }

        public MyHandler(Looper looper,HandlerThreadActivity mainActivity){
            super(looper);
            mActivity =new WeakReference<HandlerThreadActivity>(mainActivity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(null != mActivity.get()) {
                switch (msg.arg1) {
                    case HandlerThreadActivity.TYPE_BACKGROUND_UPDATE: {
                        // 这部分代码在子线程 下执行
                        Log.d(TAG, "Work Handler 参数接收: " + msg.arg2 + " thread : " + Thread.currentThread().getName());
                    }
                    break;
                    case HandlerThreadActivity.TYPE_UI_UPDATE: {
                        // 这部分代码在 UI 线程 下执行
                        String uiMsg = "UI Handler 参数接收: " + msg.arg2 + " thread : " + Thread.currentThread().getName();
                        Log.d(TAG, uiMsg);
                        mActivity.get().txt_info.setText(uiMsg);
                    }
                    break;
                }
            }
        }
    }
}
